package views
{
	import assists.HtmlText;
	import assists.log.Log;
	import assists.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.GeolocationEvent;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.sensors.Geolocation;
	
	import inerface.IGuidePanel;
	
	import main.ClientConstants;
	import main.ViewSubBase;
	
	import panel.guide.GuidePanel;
	
	import struct.CityStruct;
	
	public class GuideView extends ViewSubBase
	{
		private var _panel:IGuidePanel
		
		private var geoloc:Geolocation = new Geolocation;
		private var sender:URLLoader = new URLLoader();
		
		public function GuideView()
		{
			super();
		}
		
		public override function show():void
		{
			render();
		}
		
		protected override function render():void
		{
			if(_panel == null){
				_panel = new GuidePanel();
				_panel.init(view.layout.stage,resourceLoader,lang);
				_panel.render(openHome,openViewSpot,openEat,openShop,openHotel);
				
				_panel.onReturn = layout.closeCurrent;
				_panel.onShowList = showDownload;
				_panel.onShowWeather = showWeather;
				
				_panel.logInfo = HtmlText.formatSizeColor(data.adLogData.adLogInfo,0xff0000,18);
				_panel.titelInfo = HtmlText.formatSizeColor("<b>首页</b>",0xfff100,28);
				this.addView(_panel);
			}
			
			setCityInfo();
			
		}
		
		public function setCityInfo():void
		{
			_panel.currentCityInfo = data.cityList.currentCityInfo;
			
			getWeatherInfo();
		}
		
		private function openHome():void
		{
			trace("openHome");
			view.mainPanel.show();
			//			_panel.titelInfo = HtmlText.formatSizeColor("<b>主页</b>",0xfff100,28);
			
			changeState(true);
		}
		
		public function changeState(bool:Boolean):void
		{
			_panel.showCityChoose = bool;
		}
		
		private function openViewSpot():void
		{
			trace("openViewSpot");
			view.viewSpot.show();
			_panel.titelInfo = HtmlText.formatSizeColor("<b>景</b>",0xfff100,28);
		}
		
		private function openEat():void
		{
			trace("openeat");
			view.eating.type = ClientConstants.DETAIL_EAT;
			view.eating.show();
			_panel.titelInfo = HtmlText.formatSizeColor("<b>吃</b>",0xfff100,28);
		}
		
		private function openShop():void
		{
			trace("openshop");
			view.scenicShop.show();
			_panel.titelInfo = HtmlText.formatSizeColor("<b>娱乐</b>",0xfff100,28);
		}
		
		private function openHotel():void
		{
			view.hotel.show();
			_panel.titelInfo = HtmlText.formatSizeColor("<b>宾馆</b>",0xfff100,28);
			
		}
		
		private function getWeatherInfo():void
		{
			var city:CityStruct = data.cityList.currentCityInfo;
			if(city){
				var weatherId:int = city.weather;
				
				var url:String = "http://m.weather.com.cn/data/" + weatherId + ".html";                                                   //中央气象台API接口
				sender.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
				sender.addEventListener(Event.COMPLETE,onWeatherResult);
				sender.load(new URLRequest(url));
			}
		}
		
		protected function errorHandler(event:IOErrorEvent):void
		{
			sender.removeEventListener(Event.COMPLETE,onWeatherResult);
			sender.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			
			Log.trace("天气信息错误" + event.errorID);
		}		
		
		protected function onWeatherResult(e:Event):void
		{
			sender.removeEventListener(Event.COMPLETE,onWeatherResult);
			sender.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			
			var weatherObj:Object = assists.serialization.json.JSON.decode(sender.data);
			var str:String = weatherObj.weatherinfo. weather1 + "\n" + weatherObj.weatherinfo.temp1;
			
			data.weather.setWeather(weatherObj);
			
			_panel.weatherInfo = HtmlText.formatSizeColor(str,0x000000,14);
		}
		
		public override function close():void
		{
			return;                                                     //此导航层不能关闭
		}
		
		private function showMain():void
		{
			layout.closeCurrent();
			view.mainPanel.show();
		}
		
		public function set showOrHide(bool:Boolean):void
		{
			if(_panel){
				_panel.showReturn = bool;
			}
		}
		
		public function upAndDownVisible(upVisible:Boolean,downVisible:Boolean):void
		{
			_panel.showUpAndDown(upVisible,downVisible);
		}
		
		public function set titleText(str:String):void
		{
			_panel.titelInfo = HtmlText.formatSizeColor(str,0xfff100,24);
		}
		
		private function showWeather():void
		{
			if(view.layout.currentShow is WeatherInfoView){
				return;
			}
			view.weatherInfo.show();
		}
		
		public function showDownload():void
		{
			view.downloadZip.show();
		}
	}
}